package com.shd.edanpei.presenter;


import android.text.TextUtils;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.presenter.contract.SchemeContract;
import com.shd.edanpei.enums.StatusEnum;
import com.shd.edanpei.util.ToastUtil;

import java.util.WeakHashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/4/12.
 * yang-wen-wen
 */

public class SchemePresenter extends BasePresenterImpl<SchemeContract.SchemeIView>
        implements SchemeContract.SchemeIPresenter {


    private String mAmount;
    private String mRate;
    private String mDate;
    private String mMode;
    private String mDay;

    public SchemePresenter(SchemeContract.SchemeIView mView) {
        super(mView);
    }

    @Override
    public void setAmount(final String data) {
        this.mAmount = data;
    }

    @Override
    public void selectRate(final int id) {
        DialogUtil.showRatePicker2(mBaseIView.getContext(), "预期比", new DialogUtil.Listener() {
                    @Override
                    public void done(String data) {
                        mBaseIView.setData(id, data + Constant.Unit.LI.getUnit());
                        mRate = data;

                    }
                }, Constant.Rate.MIN.getRate(), Constant.Rate.MAX.getRate(), Constant.Rate.GAINS.getRate(),
                Constant.Unit.LI.getUnit());
    }
//
//    @Override
//    public void selectWay(String str) {
//        mMode = str;
//    }

    @Override
    public void selectData(final int viewId) {
        DialogUtil.listDialog(mBaseIView.getContext(), "订单期限", R.array.repay_date, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mBaseIView.setData(viewId, data);
                mDate = data;
            }
        });
    }

    @Override
    public void setDay(final String data) {
        this.mDay = data;
    }


    @Override
    public void submit(final int tag, final int orderId, final int orderType, final int index) {
        if (isNull(mAmount, "请输入额度")) {
            return;
        }
        if (isNull(mRate, "请选择预期比")) {
            return;
        }
        if (isNull(mDate, "请输入订单期限")) {
            return;
        }
        if (isNull(mMode, "请选择归还方式")) {
            return;
        }
        if (tag == 0 && isNull(mDay, "请输入预计审核时间")) {
            return;
        }
        if (tag == 1 && isNull(mDay, "请输入预计审核时间")) {
            return;
        }

        if (tag == 0) {
            //提供初审
            WeakHashMap<String, Object> hashMap = new WeakHashMap<>();
            hashMap.put("orderType", orderType);
            hashMap.put("orderId", orderId);
            hashMap.put("firstAmount", mAmount);
            hashMap.put("firstRate", mRate);
            hashMap.put("firstTerm", mDate);
            hashMap.put("firstMode", mMode);
            hashMap.put("firstDay", mDay);

            ApiCreator.getApi().firstApproveOrder(hashMap)
                    .compose(RxTransformer.<NoDataResponse>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<NoDataResponse>() {
                        @Override
                        protected void onSuccess(NoDataResponse data) {
                            RxBus.get().send(RxBusCode.REFRESH_ORDER_STATUS, new StatusBean(index, StatusEnum.last.getStatus()));
                            mBaseIView.getContext().finish();
                            ToastUtil.showToast("初审完成");
                        }
                    });
        } else {
            //提供终审
            WeakHashMap<String, Object> hashMap = new WeakHashMap<>();
            hashMap.put("orderType", orderType);
            hashMap.put("orderId", orderId);
            hashMap.put("lastAmount", mAmount);
            hashMap.put("lastRate", mRate);
            hashMap.put("lastTerm", mDate);
            hashMap.put("lastMode", mMode);
            hashMap.put("lastDay", mDay);
            ApiCreator.getApi().endApproveOrder(hashMap)
                    .compose(RxTransformer.<NoDataResponse>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<NoDataResponse>() {
                        @Override
                        protected void onSuccess(NoDataResponse data) {
                            RxBus.get().send(RxBusCode.REFRESH_ORDER_STATUS, new StatusBean(index, StatusEnum.waitService.getStatus()));
                            mBaseIView.getContext().finish();
                            ToastUtil.showToast("终审完成");
                        }
                    });

        }
    }

    @Override
    public void setRepayWay(final TextView view) {
        DialogUtil.listDialog(mBaseIView.getContext(), "归还方式", R.array.repay_way, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mMode = data;
                mBaseIView.setTextView(view, data);
            }
        });
    }


    private boolean isNull(String str, String toast) {
        if (TextUtils.isEmpty(str)) {
            ToastUtil.showToast(toast);
            return true;
        }
        return false;
    }


}
